package com.deercrowing.takeaway.controller.admin;

import com.deercrowing.takeaway.common.constant.TokenConstant;
import com.deercrowing.takeaway.common.model.Result;
import com.deercrowing.takeaway.common.properties.JwtProperties;
import com.deercrowing.takeaway.common.util.JwtUtil;
import com.deercrowing.takeaway.vo.EmployeeEditPasswordVO;
import com.deercrowing.takeaway.vo.EmployeeLoginVO;
import com.deercrowing.takeaway.vo.EmployeesPageVO;
import com.deercrowing.takeaway.vo.EmployeesVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import com.deercrowing.takeaway.service.EmployeesService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工信息(Employees)表控制层
 *
 * @author guoyongxiang
 * @since 2024-07-05 17:13:02
 */
@Slf4j
@Validated
@RestController
@RequestMapping("admin/employees")
public class EmployeesController {
    /**
     * 服务对象
     */
    @Autowired
    private EmployeesService employeesService;

    @Autowired
    JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeesVO
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody EmployeesVO employeesVO) {
        log.info("用户信息，账户:{},密码:{}", employeesVO.getUsername(), employeesVO.getPassword());

        if (ObjectUtils.isEmpty(employeesVO)){
            throw new NullPointerException();
        }

        /**
         * 返回对象信息将账号、密码封装至Token返回
         */
        EmployeesVO employee = this.employeesService.login(employeesVO);

        Map<String,Object> userinfo = new HashMap<>();
        userinfo.put(TokenConstant.EM_ID,employee.getId());
        userinfo.put(TokenConstant.USERNAME,employee.getUsername());
        userinfo.put(TokenConstant.NAME,employee.getName());

        String Token = JwtUtil.BuildToken(jwtProperties.getEncodedSecretKey(),jwtProperties.getLifeSpan(),userinfo);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .name(employee.getName())
                .username(employee.getUsername())
                .token(Token)
                .build();

        return ResponseEntity.ok(Result.OK(employeeLoginVO));
    }

    /**
     * 分页查询所有数据
     *
     * @param employeePageVO 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public ResponseEntity selectAll(EmployeesPageVO employeePageVO) {
        return ResponseEntity.ok(Result.OK(this.employeesService.searchByCondition(employeePageVO)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public ResponseEntity selectOne(@PathVariable @NotNull(message = "主键不能为空") Long id) {
        if (ObjectUtils.isEmpty(this.employeesService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.employeesService.selectById(id)));
    }

    /**
     * 新增数据
     *
     * @param employeesVO 实体对象
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity insert(@RequestBody EmployeesVO employeesVO) {
        if (ObjectUtils.isEmpty(employeesVO)){
            throw new NullPointerException();
        }

        this.employeesService.insertInfoToDB(employeesVO);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改数据
     *
     * @param id 主键, employeeEntity 实体对象
     * @return 修改结果
     */
    @PutMapping("/{id}")
    public ResponseEntity update(@PathVariable @NotNull(message = "主键不能为空") Long id, @RequestBody EmployeesVO employeesVO) {

        this.employeesService.updateInfoToDB(id,employeesVO);
        return ResponseEntity.ok(Result.OK());
    }
    
    /**
     * 通过主键删除单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @DeleteMapping("/{id}")
    public ResponseEntity delete(@PathVariable @NotNull(message = "主键不能为空") Long id) {
        if (ObjectUtils.isEmpty(this.employeesService.getById(id)))
        {
            throw new NullPointerException();
        }
        return ResponseEntity.ok(Result.OK(this.employeesService.removeById(id)));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public ResponseEntity delete(@RequestParam("idList") List<Long> idList) {
        return ResponseEntity.ok(Result.OK(this.employeesService.removeByIds(idList)));
    }

    /**
     * 修改员工状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/status")
    public ResponseEntity statusUpdate(@RequestParam @NotNull(message = "主键不能为空") Integer id,
                                       @RequestParam @NotNull(message = "状态不能为空") Short status){

        this.employeesService.employeeStatusUpdate(status,id);
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 登出
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity logout(){
        return ResponseEntity.ok(Result.OK());
    }

    /**
     * 修改密码
     *
     * @param employeeEditPasswordVO
     * @return
     */
    @PutMapping("/editPassword")
    public ResponseEntity changePassword(@RequestBody EmployeeEditPasswordVO employeeEditPasswordVO){
        this.employeesService.changePassword(employeeEditPasswordVO);
        return ResponseEntity.ok(Result.OK());
    }
}

